import ConfigParser
from os import path
import platform as pf

if 'windows' in pf.platform().lower():
    platform = 'windows'
else:
    platform = 'linux'

config = ConfigParser.SafeConfigParser({
    'database':
    'pxe.sqlite',
    'cfg_path':
    '/srv/tftpboot/pxelinux.cfg/os',
    'pxe_path':
    '/srv/tftpboot/pxelinux.cfg'
})

ROOT = path.dirname(__file__)
config_f = path.normpath(path.join(ROOT, 'cfg.ini'))
config_s = platform
print 'read cfg from section [%s] in %s' % (config_s, config_f)


PROJECT_ROOT = path.abspath(path.dirname(__file__))
log_path = path.join(PROJECT_ROOT, 'logs').replace('\\', '/')

def _getset(option, value=None):
    if value:
        config.set(config_s, option, value)
        with open(config_f, 'w') as f:
            config.write(f)
    config.read(config_f)
    value = config.get(config_s, option)
    if option == 'database':
        value = path.normpath(path.join(ROOT, value))
    return value


def database(value=None):
    return _getset('database', value)


def cfg_path(value=None):
    return _getset('cfg_path', value)


def pxe_path(value=None):
    return _getset('pxe_path', value)


def test():
    print cfg_path()
    print pxe_path()
    print cfg_path('xx/yy')
    print pxe_path('zz/66')
    print cfg_path()
    print pxe_path()